package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.Comment;
import com.kt.uokos.yimo.example.CommentExample;
import com.kt.uokos.yimo.mapper.CommentMapper;
import com.kt.uokos.yimo.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public List<Comment> queryByPid(Integer id,Integer offset,Integer limit,String sort,String order) {
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();
        criteria.andValueIdEqualTo(id).andTypeEqualTo((byte)0).andDeletedEqualTo(false);
        PageHelper.startPage(offset,limit);
        return commentMapper.selectByExample(example);
    }

    @Override
    public List<Comment> queryList(Integer userId, Integer valueId, Integer page, Integer limit, String sort, String order) {
        CommentExample example = new CommentExample();
        CommentExample.Criteria criteria = example.createCriteria();

        criteria.andTypeNotEqualTo((byte)2);

        if (!StringUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }

        if (!StringUtils.isEmpty(valueId)){
            criteria.andValueIdEqualTo(valueId);
        }

        criteria.andTypeEqualTo((byte)0).andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+" "+order);
        }

        PageHelper.startPage(page,limit);
        return commentMapper.selectByExample(example);
    }

    @Override
    public void delete(Comment comment) {
        commentMapper.deleteByPrimaryKey(comment.getId());

    }
}
